﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Helpers;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TGSVA
    {

        public TGSVA()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VGSMATK();
            DataContext = MV;
            InitializeComponent();
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                KEY = "";
            }
            else
            {
                KEY = dwMaster["VAFKOLL"].ToString() + dwMaster["VAFART"].ToString() + dwMaster["VAFFARBE"].ToString() + dwMaster["VAKBKUNDNR"].ToString();
            }

        }
        public override void OnCARTChg(string cart)
        {
            TBMain.Clear();
            if (cart.Length < 29)
                return;
            dwData = TMV.VA.TBMain.NewRow();
            dwData["VAAMANDANT"] = app.Mandant;
            dwData["VAAKOLL"] = cart.Substring(0, 5);
            dwData["VAAART"] = cart.Substring(5, 24);
            SResult = TMV.QueryGSMATK(dwData, 5);
            SResult.Merge(TBMain.Clone());
            GetInfoVAF(SResult.Tables[0]);
            MergeData(SResult, false, false);
        }

        public void GetInfoVAF(DataTable tbva)
        {
            
            if (tbva.Rows.Count == 0)
                return;
            string c = "";
            string yc = "";

            foreach (DataRow dwVA in tbva.Rows)
            {
                dwData = TBMain.NewRow();
                dwData["GSMMANDANT"] = app.Mandant;
                dwData["GSMART"] = dwVA["VAFFARBE"].ToString().PadRight(20); 
                DataSet set = TMV.QueryGSMATK(dwData, 6);
               string info = "";
                foreach (DataRow gs in set.Tables[0].Rows)
                {
                    string sv = gs["GSMRANT"].ToString();
                    if (sv == "")
                        sv = "0";
                    if (sv == "0") break;

                    info += Convert.ToDouble(sv) * 0.1 + gs["GSMRBEZ"].ToString().Trim().PadRight(3) + ";";
                    dwVA["COMP"] = info;
                }
            }
        }

        public override void OnDESChg(string des)
        {
            string[] str = des.Split(',');
            filter = "";
            TBTemp = TBMain.Copy();
            foreach (string comp in str)
            {
                filter += " COMP LIKE '%" + comp.Trim().PadRight(3) + "%' AND";
                foreach (DataRow row in TBTemp.Rows)
                {
                    row["COMP"] = row["COMP"].ToString().Replace(comp.Trim().PadRight(3)+";","");
                }
            }
            filter = filter + "***";
            filter = filter.Replace("AND***", "");
            cvMaster.CustomFilter = filter;
           
            int cout = 0;
            string processed = "";
            int var = -1;
            filter = "COMP NOT LIKE '%;%'";
            rows = TBTemp.Select(filter);
            foreach (DataRow row in rows)
            {
                string tvar = row["GSMVAR"].ToString();
                if (tvar.Trim() == "")
                    tvar = "-1";
                if (Convert.ToInt16(tvar) > var)
                {
                    var = Convert.ToInt16(tvar);
                }
            }
            VAR =Helper.Left( (var + 1).ToString().PadRight(3),3);
            base.OnDESChg(des);
        }
    }
}
